package com.tvunetworks.center.device.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.annotation.Logined;
import com.tvunetworks.center.device.constant.CommandConstant;
import com.tvunetworks.center.device.model.json.SetBitrateAndDelayParam;
import com.tvunetworks.center.device.model.param.*;
import com.tvunetworks.center.device.service.OtherFeignInterfaceService;
import com.tvunetworks.center.device.service.ReceiverControlService;
import com.tvunetworks.center.device.util.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @program: tvucc-device
 * @description: 和R交互命令接口
 * @author lebronchen
 * @create: 2019-03-15 13:21
 **/
@RestController
    @RequestMapping("/receiver")
@Slf4j
public class  ReceiverControlController {

    @Autowired
    private ReceiverControlService receiverControlService;

    @Autowired
    private OtherFeignInterfaceService otherFeignInterfaceService;
    /**
     * 开始live
     * @param loginUser loginUser
     * @param rid rid
     * @param sourceId sourceId
     * @return Result
     */
    @PostMapping("/startLive")
    public Result<String> startLive(@Logined LoginUser loginUser, @RequestParam String rid, @RequestParam String sourceId){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        log.error("start live param rid:"+rid+",sourceId:"+sourceId);
        return receiverControlService.startLive(loginUser.getId(), loginUser.getRole(), rid, sourceId);
    }

    /**
     * 根据peerId, 可以发送slug信息至search服务
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("/sendSlugToSearch")
    public Result<String> gridAndReceiverSendMessageToSearch(@Logined LoginUser loginUser, @RequestBody SendSlugMessageParam param){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        log.error("start live param rid and gridId  sendMessage to Search:"+param.getId()+",gridId:"+param.getSourceId());
        return receiverControlService.gridAndReceiverSendMessageToSearch(loginUser.getId(), param);
    }

    /**
     * 通过Grid查询最新的slug信息
     * @param loginUser loginUser
     * @param param param
     * @throws Exception  编译异常
     * @return  slug信息
     */
    @PostMapping("/getSlugByGridId")
    public Result<String> getSlugByGridId(@Logined LoginUser loginUser, @RequestBody SendSlugMessageParam param) throws Exception {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        String sourceId = param.getSourceId();
        return receiverControlService.getSlugByGridId(sourceId);
    }

 /*   *//**
     * 删除slug信息
     * @param loginUser loginUser
     * @param param param
     * @return  Result
     *//*
    @PostMapping("/deleteGridSlug")
    public Result<String> deleteGridSlug(@Logined LoginUser loginUser, @RequestBody SendSlugMessageParam param){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return receiverControlService.deleteGridSlug("8a8c96d951320921015133dac5790003", param);
    }*/

    /**
     *
     * @param loginUser loginUser
     * @param rid rid
     * @param sourceId sourceId
     * @param type type
     * @return Result
     */
    @PostMapping("/startLiveExtendSource")
    public Result<String> startLiveExtendSource(@Logined LoginUser loginUser, @RequestParam String rid, @RequestParam String sourceId,@RequestParam String type){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return receiverControlService.startLiveExtendSource(loginUser.getId(), loginUser.getRole(), rid, sourceId,type);
    }

    /**
     * 停止直播
     * @param loginUser loginUser
     * @param rid rid
     * @return Result
     */
    @PostMapping("/stopLive")
    public Result<String> stopLive(@Logined LoginUser loginUser, @RequestParam String rid){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        log.error("stop live param rid:"+rid);
        return receiverControlService.stopLive(loginUser.getId(), loginUser.getRole(), rid);
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/setBitrateAndDelay")
    public Result<String> setBitrateAndDelay(@Valid SetBitrateAndDelayParam param){
        return receiverControlService.setBitrateAndDelay(param);
    }

    /**
     *
     * @param rid rid
     * @param data 描述信息
     * @return Result
     */
    @PostMapping("/setMonitorSlots")
    public Result<String> setMonitorSlots(@RequestParam String rid, @RequestParam String data){
        return receiverControlService.command(rid, CommandConstant.SET_MONITOR_SLOTS, data);
    }

    /**
     * s上传EncoderLogo
     * @param rid rid
     * @param params 描述信息
     * @return Result
     */
    @PostMapping("/setEncoderLogo")
    public Result<String> setEncoderLogo(@RequestParam String rid, @RequestParam("params") String params){
        return receiverControlService.command(rid, CommandConstant.SET_STUDIO_ENCODER_LOGO, params);
    }

    /**
     *
     * @param rid rid
     * @param params 描述信息
     * @return Result
     */
    @PostMapping("/setMute")
    public Result<String> setMute(@RequestParam String rid, @RequestParam("params") String params){
        return receiverControlService.command(rid, CommandConstant.SET_MUTE, params);
    }

    /**
     *
     * @param rid rid
     * @param params 描述信息
     * @return Result
     */
    @PostMapping("/setVolume")
    public Result<String> setVolume(@RequestParam String rid, @RequestParam("params") String params){
        return receiverControlService.command(rid, CommandConstant.SET_VOLUME, params);
    }

    /**
     * 增加Glink
     * @param loginUser loginUser
     * @param gLinkParam gLinkParam
     * @return Result
     */
    @PostMapping("/addGLink")
    public Result<String> addGLink(@Logined LoginUser loginUser, GLinkParam gLinkParam){

        System.out.println("");
        if(loginUser ==null){
            return ResultUtil.noLogin();
        }
        try {
            return receiverControlService.addGLink(loginUser.getId(), gLinkParam);
        } catch (Exception e) {
            log.error("addGLink Exception:[{}]",e);
        }
        return ResultUtil.error("add glink fail");
    }

    /**
     * 编辑Glink
     * @param loginUser loginUser
     * @param gLinkParam gLinkParam
     * @return Result
     */
    @PostMapping("/editGLink")
    public Result<String> editGLink(@Logined LoginUser loginUser, GLinkParam gLinkParam){
        try {
            return receiverControlService.editGLink(loginUser.getId(),gLinkParam);
        } catch (Exception e) {
            log.error("EditGLink Exception:[{}]",e);
        }
        return ResultUtil.error("Edit glink fail");
    }

    /**
     * 删除Glink
     * @param loginUser loginUser
     * @param gLinkParam gLinkParam
     * @return  Result
     */
    @PostMapping("/deleteGLink")
    public Result<String> deleteGLink(@Logined LoginUser loginUser,GLinkParam gLinkParam){
        try {
            return receiverControlService.deleteGLink(loginUser.getId(),gLinkParam);
        } catch (Exception e) {
            log.error("deleteGLink Exception:[{}]",e);
        }
        return ResultUtil.error("Delete glink fail");
    }

    /**
     *
     * @param loginUser loginUser
     * @param rid rid
     * @param params params
     * @return Result
     */
    @PostMapping("/getModuleInfo")
    public Result<String> addRTIL(@Logined LoginUser loginUser,@RequestParam String rid, @RequestParam("params") String params){
        if(StringUtils.isEmpty(rid)){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.getModuleInfo(loginUser.getId(),rid, params);
    }

    /**
     *
     * @param loginUser loginUser
     * @param rid  rid
     * @param params params
     * @return Result
     */
    @PostMapping("/getCoreInfo")
    public Result<String> getCoreInfo(@Logined LoginUser loginUser,@RequestParam String rid, @RequestParam("params") String params){
        if(StringUtils.isEmpty(rid)){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.getCoreInfo(loginUser.getId(),rid, params);
    }

    /**
     *
     * @param loginUser loginUser
     * @param rid rid
     * @param params params
     * @return Result
     */
    @PostMapping("/getAllStatus")
    public Result<String> getAllStatus(@Logined LoginUser loginUser,@RequestParam String rid, @RequestParam("params") String params){
        if(StringUtils.isEmpty(rid)){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.getAllStatus(loginUser.getId(),rid, params);
    }

    /**
     *
     * @param loginUser loginUser
     * @param switchCodeParam switchCodeParam
     * @return Result
     */
    @PostMapping("/switchCodec")
    public Result<String> switchCodec(@Logined LoginUser loginUser, SwitchCodeParam switchCodeParam){
        if(StringUtils.isEmpty(switchCodeParam.getRid())){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.switchCodec(loginUser.getId(),switchCodeParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param switchPackEncoderSourceParam switchPackEncoderSourceParam
     * @return Result
     */
    @PostMapping("/switchPackEncoderSource")
    public Result<String> switchPackEncoderSource(@Logined LoginUser loginUser, SwitchPackEncoderSourceParam switchPackEncoderSourceParam){
        if(StringUtils.isEmpty(switchPackEncoderSourceParam.getRid())){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.switchPackEncoderSource(loginUser.getId(),switchPackEncoderSourceParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param rid rid
     * @param params params
     * @return Result
     */
    @PostMapping("/getCommandConfig")
    public Result<String> getCommandConfig(@Logined LoginUser loginUser,@RequestParam String rid, @RequestParam("params") String params){
        if(StringUtils.isEmpty(rid)){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.getCommandConfig(loginUser.getId(),rid, params);
    }

    /**
     *
     * @param loginUser loginUser
     * @param params params
     * @return Result
     */
    @PostMapping("/updateCommandConfig")
    public Result<String> updateCommandConfig(@Logined LoginUser loginUser, UpdateCommandConfigParam params){
        if(StringUtils.isEmpty(params.getRid())){
            return ResultUtil.error("rid can not be null or emptye");
        }
        return receiverControlService.updateCommandConfig(loginUser.getId(), params);
    }

    /**
     *
     * @param loginUser loginUser
     * @param rid rid
     * @param params params
     * @return Result
     */
    @PostMapping("/getBitrateRange")
    public Result<String> getBitrateRange(@Logined LoginUser loginUser,@RequestParam String rid, @RequestParam("params") String params){
        return receiverControlService.getBitrateRange(loginUser.getId(),rid, params);
    }

    /**
     *
     * @param loginUser loginUser
     * @param rid rid
     * @return Result
     */
    @PostMapping("/queryConfig")
    public Result<String> queryConfig(@Logined LoginUser loginUser,@RequestParam("rid") String rid){
        return receiverControlService.queryConfig(loginUser.getId(),rid);
    }

    /**
     * switchArea
     * @param loginUser loginUser
     * @param switchAreaParam switchAreaParam
     * @return Result
     */
    @PostMapping("/switchArea")
    public Result<String> switchArea(@Logined LoginUser loginUser, @RequestBody SwitchAreaParam switchAreaParam){
        return receiverControlService.switchArea(loginUser.getId(),switchAreaParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param switchAreaParam switchAreaParam
     * @return Result
     */
    @PostMapping("/coreInfo")
    public Result<String> coreInfo(@Logined LoginUser loginUser, @RequestBody SwitchAreaParam switchAreaParam){
        return receiverControlService.coreInfo(loginUser.getId(),switchAreaParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param switchAreaParam switchAreaParam
     * @return Result
     */
    @PostMapping("/getLiveVoIP")
    public Result<String> getLiveVoIP(@Logined LoginUser loginUser,@RequestBody  SwitchAreaParam switchAreaParam){
        return receiverControlService.getLiveVoIP(loginUser.getId(),switchAreaParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param liveVoipParam liveVoipParam
     * @return Result
     */
    @PostMapping("/setLiveVoIP")
    public Result<String> setLiveVoIP(@Logined LoginUser loginUser,@RequestBody  LiveVoipParam liveVoipParam){
        return receiverControlService.setLiveVoIP(loginUser.getId(),liveVoipParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param voipVolumeParam voipVolumeParam
     * @return Result
     */
    @PostMapping("/getVoipVolume")
    public Result<String> getVoipVolume(@Logined LoginUser loginUser, @RequestBody VoipVolumeParam voipVolumeParam){
        return receiverControlService.getVoipVolume(loginUser.getId(),voipVolumeParam);
    }

    /**
     *
     * @param loginUser loginUser
     * @param voipVolumeParam voipVolumeParam
     * @return Result
     */
    @PostMapping("/getGridEncodeVideoUrl")
    public Result<String> getGridEncodeVideoUrl(@Logined LoginUser loginUser,@RequestBody  VoipVolumeParam voipVolumeParam){
        return receiverControlService.getGridEncodeVideoUrl(loginUser.getId(),voipVolumeParam);
    }

    /**
     *
     * @param rid rid
     * @param params params
     * @param url url
     * @return Result
     */
    @PostMapping("/setEncoderLogoForPlayout")
    public Result<String> setEncoderLogoForPlayout(@RequestParam String rid, @RequestParam("params") String params,@RequestParam String url){
        //校验url合法性
        if(StringUtils.isEmpty(url)){
            return ResultUtil.paramError("Image url can not be null or empty.");
        }
        if(StringUtils.isEmpty(rid)){
            return ResultUtil.paramError("Rid  can not be null or empty.");
        }
        String logoFileBase64String = ImageUtils.image2Base64(url);

        if(StringUtils.isEmpty(logoFileBase64String)){
             return ResultUtil.paramError("Image not exist.");
        }
        params =  params.replace("XXX",logoFileBase64String);
        return receiverControlService.command(rid, CommandConstant.SET_STUDIO_ENCODER_LOGO, params);
    }

    /**
     * 清楚EncoderLogo
     * @param rid rid
     * @param params params
     * @return Result
     */
    @PostMapping("/clearEncoderLogo")
    public Result<String> clearEncoderLogo(@RequestParam String rid, @RequestParam("params") String params){
        return receiverControlService.command(rid, CommandConstant.SET_STUDIO_ENCODER_LOGO_CLEAR, params);
    }

    /**
     * 重启linuxR
     * @param loginUser loginUser
     * @param rid rid
     * @return Result
     */
    @PostMapping("/restartLinuxR")
    public Result restartLinuxR(@Logined LoginUser loginUser, @RequestParam("rid") String rid) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        return receiverControlService.restartOrRebootLinuxR(rid, CommandConstant.RESTART_LINUX_R);
    }

    /**
     * 重启R所在的Linux机器
     * @param loginUser loginUser
     * @param rid rid
     * @return Result
     */
    @PostMapping("/rebootLinuxR")
    public Result rebootLinuxR(@Logined LoginUser loginUser, @RequestParam("rid") String rid) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        return receiverControlService.restartOrRebootLinuxR(rid, CommandConstant.REBOOT_LINUX);
    }

    /**
     * 传递ecopreview信息
     * @param loginUser loginUser
     * @param ecoPreview 描述信息
     * @return Result
     */
    @PostMapping("/ecoPreview")
    public Result ecoPreview(@Logined LoginUser loginUser, @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }

        String data = ecoPreview.getData();
        String rid = ecoPreview.getRid();
        return receiverControlService.ecoPreview(rid, CommandConstant.ECO_PREVIEW, data);
    }

    /**
     * 获取ecopreview信息
     * @param loginUser loginUser
     * @param ecoPreview 描 述信息
     * @return Result
     * @throws IOException 编译异常
     */
    @PostMapping("/GetEcoPreviewInfo")
    public Result getEcoPreviewInfo(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) throws IOException {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        return receiverControlService.getEcoPreviewInfo(rid, CommandConstant.GET_ECO_PREVIEW);

    }

    /**
     * 关闭ecopreview
     * @param loginUser loginUser
     * @param ecoPreview ecoPreview
     * @return Result
     */
    @PostMapping("/exitEcoPreviewInfo")
    public Result exitEcoPreviewInfo(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        return receiverControlService.exitEcoPreviewInfo(rid, CommandConstant.EXIT_ECO_PREVIEW);
    }

    /**
     * 获取encoderLogo
     * @param loginUser loginUser
     * @param ecoPreview ecoPreview
     * @return Result
     */
    @PostMapping("/getEncoderLogo")
    public Result getEncoderLogo(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();

        String data=ecoPreview.getData();

        return receiverControlService.getEncoderLogo(rid, CommandConstant.GET_LOGO_URL,data);

    }

    /**
     * 获取Transcriber开关信息
     * @param loginUser loginUser
     * @param ecoPreview ecoPreview
     * @return Result
     */
   //200-get smart caption settings
    @PostMapping("/getSmartCaptionSettings")
    public Result getSmartCaptionSettings(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        //方法复用,传参不同
        return receiverControlService.exitEcoPreviewInfo(rid, CommandConstant.GET_SMART_CAPTION_SETTINGS);

    }


    /**
     *  传递Transcriber开关信息
     * @param loginUser loginUser
     * @param ecoPreview ecoPreview
     * @return Result
     */
  // 201-SetSmartCaption
    @PostMapping("/setSmartCaption")
    public Result setSmartCaption(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        String data = ecoPreview.getData();
        //方法复用,传参不同
        return receiverControlService.getEncoderLogo(rid, CommandConstant.SET_SMART_CAPTION_SETTINGS,data);

    }

    /**
     * 获取目前字幕替换规则
     * @param loginUser loginUser
     * @param ecoPreview ecoPreview
     * @return Result
     */
    // 300-GetReplace
    @PostMapping("/getReplace")
    public Result getReplace(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        //方法复用,传参不同
        return receiverControlService.exitEcoPreviewInfo(rid, CommandConstant.GET_REPLACE);

    }

    /**
     * 传入限制字幕
     * @param loginUser loginUser
     * @param ecoPreview 描述信息
     * @return Result
     */
    // 300-GetReplace
    @PostMapping("/setReplace")
    public Result setReplace(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        String data = ecoPreview.getData();
        //方法复用,传参不同
        return receiverControlService.getEncoderLogo(rid, CommandConstant.SET_REPLACE,data);

    }

    /**
     * 获取Scheduled
     * @param loginUser loginUser
     * @param ecoPreview 描述信息
     * @return Result
     */
    // 400-GetScheduled
    @PostMapping("/getScheduled")
    public Result getScheduled(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        //方法复用,传参不同
        return receiverControlService.exitEcoPreviewInfo(rid, CommandConstant.GET_SCHEDULED);

    }

    /**
     * 传入Scheduled
     * @param loginUser loginUser
     * @param ecoPreview 描述信息
     * @return Result
     */
    // 401-SetScheduled
    @PostMapping("/setScheduled")
    public Result setScheduled(@Logined LoginUser loginUser,  @RequestBody EcoPreview ecoPreview) {
        if (loginUser == null) {
            ResultUtil.noLogin();
        }
        String rid = ecoPreview.getRid();
        String data = ecoPreview.getData();
        //方法复用,传参不同
        return receiverControlService.getEncoderLogo(rid, CommandConstant.SET_SCHEDULED,data);

    }

    /**
     *
     * @param rid rid
     * @return String
     * 云识别的文案
     */
    @RequestMapping("/getFoneMessage")
    public Result<ResponseEntity<String>> getFoneMessage(@RequestParam("rid") String rid) {
        return  otherFeignInterfaceService.getFontMessage(rid);
    }
}
